<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/jsp/comm_css_include.jsp"%>
<!-- jqgrid-->
<link
	href="${basePath}/css/plugins/jqgriddemo/css/ui.jqgrid-bootstrap.min.css"
	rel="stylesheet" />
<link rel="stylesheet" type="text/css"
	href="${basePath}/css/zTreeStyle/zTreeStyle/zTreeStyle.css">
<!doctype html>
<html>
<head>
<style type="text/css">
.maxWidth60 {
	width: 60%;
}

.divfloatleft {
	float: left
}

.maxWidth40 {
	width: 40%
}
</style>
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="iboxtitle">
			<span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;系统管理&gt;基础信息管理&gt;业务员跨机构调整</span>
			<a href="javascript:void(0)"></a>
		</div>
		<div class="ibox-content">
			<div role="form" class="form-inline">
				<div class="form-group">
					<input type="text" placeholder="请输入业务员名字" id="saleman"
						class="form-control">
				</div>
				<button class="zhixcx" type="button" onclick="query();">搜索业务员名字</button>
			</div>
		</div>
		<div class="ibox-content">
			<div class="jqGrid_wrapper">
				<table id="table_list_1"></table>
				<div id="pager_list_1"></div>
			</div>
		</div>
	</div>

	<form class="form-horizontal m-t" id="commentForm">
		<div class="ibox-content" id="hiddenstyle" style="display: none;">
			<div class="form-group">
				<label class="col-sm-3 control-label">业务员姓名：</label>
				<div class="col-sm-8">
					<input id="name" name="name" minlength="1" maxlength="32"
						type="text" class="form-control" required="" aria-required="true"
						placeholder="请输入姓名">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">组织机构：</label>
				<div class="col-sm-8">
					<input id="unitId" name="unitId" type="hidden"> <input
						id="unitCode" name="unitCode" type="hidden"> <input
						id="orgName" name="orgName" readonly="readonly" type="text"
						class="form-control smalliconcur smallicon" required=""
						aria-required="true" onclick="choseOrg();" placeholder="请选择机构">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="remarks" name="remarks" class="form-control"
						aria-required="true" placeholder="请输入备注"></textarea>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-3">
					<button class="btn dpbtn_blue" type="button" onclick="save();">提交</button>
				</div>
			</div>
		</div>
	</form>

	<form class="form-horizontal m-t" id="commentFormb">
		<div class="ibox-content" id="hiddenstyleb" style="display: none;">
			<div class="form-group">
				<label class="col-sm-3 control-label">业务员姓名：</label>
				<div class="col-sm-8">
					<input id="ywid" name="id" type="hidden" /> <input
						id="salersmanCodeb" name="salersmanCode" type="hidden" /> <input
						id="nameb" readonly="readonly" name="name" minlength="1"
						maxlength="32" type="text" class="form-control" required=""
						aria-required="true" placeholder="请输入姓名">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">组织机构：</label>
				<div class="col-sm-8">
					<input id="oldUnitId" name="oldUnitId" type="hidden" /> <input
						id="oldUnitCode" name="oldUnitCode" type="hidden" /> <input
						id="oldUnitName" name="oldUnitName" type="hidden" /> <input
						id="unitIdb" name="unitId" type="hidden" /> <input id="unitCodeb"
						name="unitCode" type="hidden" /> <input id="orgNameb"
						name="orgName" readonly="readonly" type="text"
						class="form-control smalliconcur smallicon" required=""
						aria-required="true" onclick="choseOrg();" placeholder="请选择组织机构">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="remarksb" name="remarks" readonly="readonly"
						class="form-control" aria-required="true" placeholder="请输入备注"></textarea>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-3">
					<button class="btn dpbtn_blue" type="button" onclick="doUpdate();">提交</button>
				</div>
			</div>
		</div>
	</form>
	<!-- public quote -->
	<%@include file="/jsp/comm_js_include.jsp"%>

	<!-- jqGrid -->
	<script type="text/javascript" charset="utf-8"
		src="${basePath}/js/plugins/jqgriddemo/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" charset="utf-8"
		src="${basePath}/js/plugins/jqgriddemo/js/grid.locale-cn.min.js"></script>

	<!-- custom js -->
	<%@include
		file="/js/common-dpqc/dpqc-validate/comm_validate_include.jsp"%>
	<!--zTreeStyle.js-->
	<script type="text/javascript"
		src="${basePath}/js/zTreeStyle/jquery.ztree.core.js" charset="utf-8"></script>
	<script type="text/javascript"
		src="${basePath}/js/zTreeStyle/jquery.ztree.excheck.js"
		charset="utf-8"></script>

	<!--
    	作者：zhangpp
    	时间：2017-04-06
    	描述：js
    -->
	<script type="text/javascript">
    
		var winWidth = $(window).width();
		var winHeight = $(window).height();
	    jQuery(function($) {  
	    	getData();
	    });
	    var changerlayer;
	    var getData = function()
	    {
	       winWidth = $(window).width();
       	   var winHeight = document.body.offsetHeight - 300;
    	   var win = winWidth / 7 - 20;
  	 	   $.jgrid.defaults.styleUI = 'Bootstrap';
  	 	   var data={};
		  jQuery("#table_list_1").jqGrid({
			  url:'${basePath}/BaseSalersmanController/selectMuiltList',
	        datatype : "json",
	        colNames : [ '主键', '业务员名字', '机构名称'],
	        colModel : [ 
	                     {name: 'replaceId',index: 'replaceId', hidden:true},
	            		 {name: 'name',index: 'name',width: win,align: "center",sorttype: "float"},
	               		 {name: 'orgName',index: 'orgName',width: win,align: "center",sorttype: "float"}
	                   ],
              mtype : "post",
              height: winHeight,
              width:winWidth,  
              autowidth: true,
              shrinkToFit: true,
              rowNum: 15,
	           rowList: [15, 30, 45],
              viewrecords: true,
              pager: "#pager_list_1",
              hidegrid: false,
              multiselect : true,		//实现单选    multiboxonly:true   beforeSelectRow: true,
              multiboxonly:false,  
              altRows:true,
              altclass:'someClass',
              ondblClickRow:function(rowId,v,e)
              {
            	  var rowData = $("#table_list_1").jqGrid("getRowData", rowId);
            	  var id = rowData.replaceId;
            	  document.getElementById("ywid").value=id;
	      	      $.dpAjax({
	      	    		url:'${basePath}/BaseSalersmanController/selectMuiltList',
	      				data:{
	         	   			"id":id
	         	   		},
	      				success: function(datas){
	      		    	//页面层
	      		    	var result = datas.result.rows[0];
	      		    	changerlayer = layer.open({
	      			    	  title:'业务员跨机构修改',
	      			    	  type: 1,
	      			    	  area: ['480px', '50%'], //宽高
	      		   			  content: $('#hiddenstyleb')
	      			    	});
	      		    		$("#oldUnitId").val(result.unitId);
	      		    		$("#oldUnitCode").val(result.unitCode);
	      		    		$("#oldUnitName").val(result.orgName);
	      		    		document.getElementById("salersmanCodeb").value=result.salersmanCode;
	      					document.getElementById("nameb").value=result.name;
	      					document.getElementById("unitIdb").value=result.unitId;
	      					document.getElementById("unitCodeb").value=result.unitCode;
	      					document.getElementById("orgNameb").value=result.orgName;
	      					document.getElementById("remarksb").value=result.remarks;
	                  	}
	      	      });
              },
              jsonReader : {
            	  root:"result.rows",
            	  page: "result.page",
            	  total: "result.total",
            	  records: "result.records",
            	  repeatitems: false,
            	  id: "replaceId"
              },
              loadComplete:function(datas){
            	  if (datas.flag == "100100") {
                      layer.msg("查询成功!", {icon: 6, time: 1000});
                      return;
                  }
            	  if(datas.flag == '100802') {
    				  top.location.href = thisLoginUrl;
    				  return;
    			  }

            	  layer.msg(datas.message, {icon: 2});
              }
	      });
	    }
	    
	    window.onresize = function()
	    {
	    	 $("#table_list_1").setGridWidth($(window).width()-80);
	    	 $("#table_list_1").setGridHeight($(window).height() / 2.5);
	    }
	    
	    //搜索
	    var query = function()
	    {
	    	var data={};
	    	data.name=document.getElementById("saleman").value.trim();
	    	$("#table_list_1").jqGrid("setGridParam",{
	    		url:'${basePath}/BaseSalersmanController/selectMuiltList',
   				postData:data,
   				datatype:"json"
	    	}).trigger("reloadGrid")
	    }
	    
	    var addlayer;
	    var add = function()
	    {
	    	  addlayer = layer.open({
	    	  title:'业务员信息录入',
	    	  type: 1,
	    	  area: ['480px', '50%'], //宽高
   			  content: $('#hiddenstyle')
	    	});
	    }
	    
	    var save = function()
	    {
 	    	var validstatus = $("#commentForm").valid();
 	    	if(validstatus)
 	    	{
	    		var data = $('#commentForm').serialize();
	    		$.dpAjax({
		    		url:'${basePath}/BaseSalersmanController/save',
		    		data:data,
	    			success: function(datas){
		    			  if(datas.flag == "100100")
						  {
							  layer.msg('执行成功！', {icon: 6});
							  layer.close(addlayer);
							  jQuery("#table_list_1").trigger("reloadGrid");
							  return;
						  }
						  if(datas.flag == "100200")
		                  {
							  $.fn.showDatas({"datas":datas});
		                	  return;
		                  }
	                 }
		    	});
 	    	}
	    }
	    
	    var disable=function(){
	    	var rowData = jQuery('#table_list_1').jqGrid('getGridParam','selarrrow');  
	    	if(rowData.length < 1)
	    	{  
	    		layer.msg("请选择要删除的数据信息！", {icon: 5});
				return;
	    	}
	    	
	    	
	    	for(var i=0;i<rowData.length;i++)
	    	{
	    		var mainId = jQuery('#table_list_1').jqGrid('getCell',rowData[i],'replaceId'); 
    			layer.confirm('您确认要删除吗？', {
    			  btn: ['确定','取消'] //按钮
    			},function(){
    				layer.closeAll();
    				$.dpAjax({
    		    		url:'${basePath}/BaseSalersmanController/disable',
    	    			data:{"id":mainId},
    	    			success: function(datas){
    	    			  if(datas.flag == "100100")
  						  {
  							  layer.msg('执行成功！', {icon: 6});
  							 layer.close(1);
							  jQuery("#table_list_1").trigger("reloadGrid");
  							  return;
  						  }
  						  if(datas.flag == "100200")
  		                  {
  							  $.fn.showDatas({"datas":datas});
  		                	  return;
  		                  }
    	                }
    		    	});
    			});
	    	}
	    }
	    var del = function()
	    {
	    	var rowData = jQuery('#table_list_1').jqGrid('getGridParam','selarrrow');  
	    	if(rowData.length < 1)
	    	{  
	    		layer.msg("请选择要删除的数据信息！", {icon: 5});
				return;
	    	}
	    	
	    	
	    	for(var i=0;i<rowData.length;i++)
	    	{
	    		var mainId = jQuery('#table_list_1').jqGrid('getCell',rowData[i],'replaceId'); 
    			layer.confirm('您确认要删除吗？', {
    			  btn: ['确定','取消'] //按钮
    			},function(){
    				layer.closeAll();
    				$.dpAjax({
    		    		url:'${basePath}/BaseSalersmanController/remove',
    	    			data:{"id":mainId},
    	    			success: function(datas){
    	    			  if(datas.flag == "100100")
  						  {
  							  layer.msg('执行成功！', {icon: 6});
  							 layer.close(1);
							  jQuery("#table_list_1").trigger("reloadGrid");
  							  return;
  						  }
  						  if(datas.flag == "100200")
  		                  {
  							  $.fn.showDatas({"datas":datas});
  		                	  return;
  		                  }
    	                }
    		    	});
    			});
	    	}
	    }
	    
	    var index;
	    var choseOrg=function(){
	    	$.dpAjax({
	    		url:'${basePath}/BaseBusiUnitController/list',
    			success: function(datas){
	    			  if(datas.flag == "100100")
					  {
	    				  var content = '<div class="ibox-content" style="height: 280px;border: 1px solid #F5F5F6; overflow:auto;overflow-x: hidden;">'
	    						+ '	<div id="orgTree" class="ztree">&nbsp;</div>'
	    					+ '</div>'
	    					+ '<div class="ibox-content">'
	    					+ '	 <button class="btn dpbtn_green" type="button" onclick="confirmPower();">确定</button>'
	    					+ '	 <button class="btn dpbtn_red" type="button" onclick="cancelPower();">取消</button>'
	    					+ '</div>';

	    				index = layer.open({
	    					  title:'机构列表',
	    					  type: 1,
	    					  area: ['300px', '400px'], //宽高
	    					  content: content
	    				});
				    	var setting = {
			 					check: {
			 						enable: false
			 					},
			 					data: {
			 						simpleData: {
			 							enable: true
			 						}
			 					}
			 			 };			
    				  var zNodes = datas.result.rows;
    				  var list=[];
    				  for(var i=0;i<datas.result.rows.length;i++){
	    				  var obj=new Object();
    					  obj.id=datas.result.rows[i].replaceId;
    					  obj.name=datas.result.rows[i].orgName;
    					  obj.pId=datas.result.rows[i].parentId;
    					  obj.unitCode=datas.result.rows[i].orgCode;
    					  if(obj.parentCode == "00"){
    	         		  	 	 obj.open = true;
    	   				  }
    					  list.push(obj);
    				  }
    				  $.fn.zTree.init($("#orgTree"), setting, list);
					  return;
					  }
					  if(datas.flag == "100200")
	                  {
						  $.fn.showDatas({"datas":datas});
	                	  return;
	                  }
                 }
	    	});
	    }
	    
	    var confirmPower = function()
	    {
	    	 var zTree = $.fn.zTree.getZTreeObj("orgTree"); 
	    	 var node=zTree.getSelectedNodes();
	    	 var treeNode = node[0];
	    	 if(treeNode.isParent){
	    		 layer.msg("父节点不可选择！", {icon: 5});
	    		 return;
	    	 }
	    	 
	    	 var parentNode = treeNode.getParentNode();
	    	 if(!parentNode) {
	    		 layer.msg("只能选择行政机构!", {icon: 5});
	    		 return;
	    	 }
	    	 document.getElementById("orgNameb").value=node[0].name;
	    	 document.getElementById("unitCodeb").value=node[0].unitCode;
	    	 document.getElementById("unitIdb").value=node[0].id;
	    	 layer.close(index); 
	    }
		
	    var cancelPower = function()
	    {
	    	layer.close(index); 
	    }
	    
	    
	    var doUpdate=function(){
    		var data = $('#commentFormb').serialize();
    		var validstatus = $("#commentFormb").valid();
  	    	if(validstatus)
  	    	{
	    		$.dpAjax({
		    		url:'${basePath}/BaseSalersmanController/update',
		    		data:data,
	    			success: function(datas){
		    			  if(datas.flag == "100100")
						  {
							  layer.msg('执行成功！', {icon: 6});
							  layer.close(changerlayer);
							  jQuery("#table_list_1").trigger("reloadGrid");
							  return;
						  }
						  if(datas.flag == "100200")
		                  {
							  $.fn.showDatas({"datas":datas});
		                	  return;
		                  }
	                 }
		    	});
  	    	}
	    }
	    
	    $(function(){
  		// 验证
  	        $("#commentForm").validate({
  	            //重写showErrors
  	            showErrors: function (errorMap, errorList) {
  	                
  	                $.each(errorList, function (i, v) {
  	                    layer.tips(v.message, v.element, { time: 1500 });
  	                    return false;
  	                });  
  	            },
  	            /* 失去焦点时验证 */
  	            onfocusout: function(element) { $(element).valid(); },
  	            /*  键盘抬起不验证 */
  	            onkeyup:false,
  	        })
  	        
  	        $("#commentFormb").validate({
  	            //重写showErrors
  	            showErrors: function (errorMap, errorList) {
  	                
  	                $.each(errorList, function (i, v) {
  	                    layer.tips(v.message, v.element, { time: 1500 });
  	                    return false;
  	                });  
  	            },
  	            /* 失去焦点时验证 */
  	            onfocusout: function(element) { $(element).valid(); },
  	            /*  键盘抬起不验证 */
  	            onkeyup:false,
  	        })
	    });
    </script>
</body>
</html>
